package org.xue.snowmall.live.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.xue.snowmall.common.utils.PageUtils;
import org.xue.snowmall.common.utils.Query;

import org.xue.snowmall.common.utils.R;
import org.xue.snowmall.live.dao.LiveRecordDao;
import org.xue.snowmall.live.entity.LiveEntity;
import org.xue.snowmall.live.entity.LiveRecordEntity;
import org.xue.snowmall.live.service.LiveRecordService;
import org.xue.snowmall.live.service.LiveService;
import org.xue.snowmall.live.vo.RelationVo;


@Service("liveRecordService")
public class LiveRecordServiceImpl extends ServiceImpl<LiveRecordDao, LiveRecordEntity> implements LiveRecordService {

    @Autowired
    private LiveService liveService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LiveRecordEntity> page = this.page(
                new Query<LiveRecordEntity>().getPage(params),
                new QueryWrapper<LiveRecordEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R saveLiveRecord(List<RelationVo> vo) {

        Long gsmLiveId = vo.get(0).getGsmLiveId();
        LiveEntity liveEntity = liveService.getById(gsmLiveId);
        List<LiveRecordEntity> collect = vo.stream().map(item -> {
            LiveRecordEntity liveRecordEntity = new LiveRecordEntity();
            BeanUtils.copyProperties(item, liveRecordEntity);
            liveRecordEntity.setGsmLiveRecordFollowersNumber(0);
            liveRecordEntity.setGsmLiveRecordOrdersNumber(0);
            liveRecordEntity.setGsmLiveRecordViewsNumber(0);
            liveRecordEntity.setGsmLiveRecordThumbNumber(0);
            liveRecordEntity.setGsmLiveRecordStartTime(new Date());
            liveRecordEntity.setGsmLivePullUrl(liveEntity.getGsmLivePullUrl());
            return liveRecordEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
        return R.ok();
    }

}